/*
 *
 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#ifdef HI_UPDATE_OTA_TEST

#include "ota_test.h"

#include <string.h>

#include <linux/kernel.h>

#include "../ota_update/ota_update.h"
#include "../ota_update/ota_package_info.h"
#include "../update_log.h"
#include "../verify/verify_sign.h"

static unsigned char g_test_info_comp_bin[] = {
	0x01, 0x00, 0x88, 0x00, 0x0B, 0x01, 0x00, 0x00,
	0x01, 0x00, 0x00, 0x00, 0x48, 0x61, 0x72, 0x6D,
	0x6F, 0x6E, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x4F, 0x70, 0x65, 0x6E,
	0x48, 0x61, 0x72, 0x6D, 0x6F, 0x6E, 0x79, 0x20,
	0x31, 0x2E, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00,
	0x32, 0x30, 0x32, 0x30, 0x2E, 0x31, 0x30, 0x2E,
	0x32, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x31, 0x37, 0x3A, 0x30, 0x35, 0x3A, 0x35, 0x37,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x05, 0x00, 0x47, 0x00, 0x6F, 0x74, 0x61, 0x5F,
	0x74, 0x61, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x05, 0x00,
	0x00, 0x31, 0x2E, 0x30, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x82, 0x09, 0x9E, 0x66, 0xDC,
	0x1E, 0x42, 0x8B, 0x86, 0x44, 0x30, 0x58, 0x64,
	0xB2, 0x04, 0x86, 0x7B, 0xEF, 0x66, 0xAD, 0xAC,
	0xAF, 0x42, 0x4F, 0x67, 0x20, 0xFD, 0x1D, 0x33,
	0xFC, 0x51, 0xEB, 0x75, 0x70, 0x64, 0x61, 0x74,
	0x65, 0x2F, 0x69, 0x6E, 0x66, 0x6F, 0x2E, 0x62,
	0x69, 0x6E, 0x00, 0x61, 0xB2, 0xA7, 0x92, 0xB3,
	0x9E, 0x67, 0x35, 0x40, 0x22, 0xAC, 0x45, 0x36,
	0x77, 0xBD, 0xF5, 0xC5, 0xEF, 0x0B, 0x78, 0x6C,
	0x5A, 0x7E, 0xD8, 0x99, 0xE9, 0x03, 0x19, 0x00,
	0x51, 0x39, 0x57, 0x63, 0x2C, 0xE5, 0x77, 0x1C,
	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0x95, 0x73, 0x8E, 0x42, 0x36,
	0x90, 0x3E, 0x00, 0xFB, 0x74, 0x46, 0x31, 0x6C,
	0xB5, 0xA7, 0xF6, 0x90, 0x5B, 0xF8, 0x94, 0xA8,
	0xBD, 0x1D, 0xF8, 0x5E, 0x9F, 0xA8, 0x72, 0x4B,
	0xD2, 0x24, 0x4B, 0xBE, 0xC8, 0xFD, 0x66, 0x06,
	0x72, 0x17, 0xE2
};

static bool test_ota_package_info_component(void)
{
	ota_init_var();
	if (parse_ota_info_component(g_test_info_comp_bin,
		sizeof(g_test_info_comp_bin)) != 0) {
		UPD_LOGE("parse fail");
		return false;
	}

	char path[OTA_PATH_LEN] = {0};
	if (change_to_ota_path(OTA_TAG_FILE, path,
		sizeof(path)) != 0) {
		UPD_LOGE("change to ota path fail");
		return false;
	}
	if (!is_component_in_ota_header(path)) {
		UPD_LOGE("ota_flag is not in ota header");
		return false;
	}

	return true;
}

static bool test_ota_img_hash_check(void)
{
	const char *img_data = "TEST-OTA";
	unsigned char dst_hash[HASH_LEN] = {
		0x82, 0x09, 0x9E, 0x66, 0xDC, 0x1E, 0x42, 0x8B,
		0x86, 0x44, 0x30, 0x58, 0x64, 0xB2, 0x04, 0x86,
		0x7B, 0xEF, 0x66, 0xAD, 0xAC, 0xAF, 0x42, 0x4F,
		0x67, 0x20, 0xFD, 0x1D, 0x33, 0xFC, 0x51, 0xEB
	}; /* from g_test_info_comp_bin */
	unsigned char hash[HASH_LEN] = {0};

	if (calc_image_hash((unsigned char *)img_data, strlen(img_data),
		hash, sizeof(hash)) != 0) {
		UPD_LOGE("get hash fail");
		return false;
	}

	if (memcmp(hash, dst_hash, HASH_LEN) != 0) {
		UPD_LOGE("compare fail");
		return false;
	}

	UPD_LOGI("check hash ok!");
	return true;
}

bool test_ota_version_check(void)
{
	struct version_check {
		bool expect;
		char pkg[24];
		char local[24];
	};
	struct version_check map[] = {
		{true,  "OpenHarmony 1.1",      "OpenHarmony 1.0"},
		{true,  "OpenHarmony 1.1.02.3", "OpenHarmony 1.01.2.0"},
		{false, "OpenHarmony 0.9",      "OpenHarmony 1.0"},
		{false, "OpenHarmony 1.01.2.3", "OpenHarmony 1.1.02.4"},
		{false, "OpenHarmo 1.1",        "OpenHarmony 1.0"},
		{false, "OpenHarmony 1.1.2.0",  "OpenHarmony 1.1.2.0"},
	};
	int i;

	for (i = 0; i < ARRAY_SIZE(map); i++) {
		if (is_higher_version(map[i].pkg, map[i].local) !=
			map[i].expect) {
			UPD_LOGE("version test %d fail", i);
			return false;
		}
	}
	return true;
}

bool test_ota_update(void)
{
	if (!test_ota_package_info_component()) {
		UPD_LOGE("test ota package veriry: FAIL");
		return false;
	}
	UPD_LOGI("test ota package veriry: PASS");

	if (!test_ota_img_hash_check()) {
		UPD_LOGE("test ota img hash check: FAIL");
		return false;
	}
	UPD_LOGI("test ota img hash check: PASS");

	if (!test_ota_version_check()) {
		UPD_LOGE("test ota version check: FAIL");
		return false;
	}
	UPD_LOGI("test ota version check: PASS");
	return true;
}

#endif /* HI_UPDATE_OTA_TEST */
